package com.waterfairy.application;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.waterfairy.media.audio.play.AudioPlayViewShowTool;
import com.waterfairy.media.audio.play.AudioPlayerView;
import com.waterfairy.media.audio.play.OnPlayClickListener;
import com.waterfairy.mp3lame.SimpleLame;
import com.waterfairy.recordaudiolibrary.AudioRecordTool;
import com.waterfairy.recordaudiolibrary.PcmToWav;
import com.waterfairy.utils.ToastUtils;

import java.io.File;
import java.io.IOException;

public class Mp3LameActivity extends AppCompatActivity {
    AudioRecordTool audioRecordTool;

    private TextView tvInfo, tvDb, tvPcmSize, tvWavSize, tvMp3Size;
    private Button btHandler, btPlayWav, btPlayMp3;

    private String pcmPath;
    private String wavPath;
    private String mp3Path;

    private AudioPlayerView audioPlayerView;

    private AudioPlayViewShowTool playViewShowTool;


    private int state = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mp3_lame);

        requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 1001);

        tvInfo = findViewById(R.id.tv_info);
        tvDb = findViewById(R.id.tv_db);
        tvPcmSize = findViewById(R.id.tv_pcm_size);
        tvWavSize = findViewById(R.id.tv_wav_size);
        tvMp3Size = findViewById(R.id.tv_mp3_size);

        audioPlayerView = findViewById(R.id.view_audio_player);
        playViewShowTool = new AudioPlayViewShowTool(audioPlayerView, false);
        playViewShowTool.setOnPlayClickListener(new OnPlayClickListener() {
            @Override
            public void onPlayClick() {

            }

            @Override
            public void onPauseClick() {

            }

            @Override
            public void onCloseClick() {

            }
        });
        btHandler = findViewById(R.id.bt_handler);
        btPlayWav = findViewById(R.id.bt_play_wav);
        btPlayMp3 = findViewById(R.id.bt_play_mp3);
        btPlayWav.setVisibility(View.GONE);
        btPlayMp3.setVisibility(View.GONE);


        btPlayWav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playViewShowTool.play(wavPath);
            }
        });

        btPlayMp3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playViewShowTool.play(mp3Path);
            }
        });

        findViewById(R.id.bt_covert_wav).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PcmToWav.makePCMFileToWAVFile(pcmPath, wavPath, false);
                ToastUtils.show("转WAV完成");
                tvWavSize.setText("size: " + new File(wavPath).length() + "");
                btPlayWav.setVisibility(View.VISIBLE);
            }
        });


        findViewById(R.id.bt_covert_mp3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SimpleLame().wavToMp3(wavPath, mp3Path, 16000, 1, 16000, 128, 1, 2);
                ToastUtils.show("转MP3完成");
                tvMp3Size.setText("size: " + new File(mp3Path).length() + "");
                btPlayMp3.setVisibility(View.VISIBLE);
            }
        });

        btHandler.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state == 0) {
                    state = 1;
                    btHandler.setText("停止");

                    tvPcmSize.setText("");
                    tvWavSize.setText("");
                    tvMp3Size.setText("");
                    btPlayWav.setVisibility(View.GONE);
                    btPlayMp3.setVisibility(View.GONE);

                    String basePath = getExternalFilesDir("file").getAbsolutePath() + "/" + System.currentTimeMillis();

                    pcmPath = basePath + ".pcm";
                    wavPath = basePath + ".wav";
                    mp3Path = basePath + ".mp3";

                    audioRecordTool.setPcmPath(pcmPath);
                    tvInfo.setText(pcmPath);
                    boolean start = audioRecordTool.start();
                    if (start) {
                        ToastUtils.show("开始录音");
                    } else {
                        ToastUtils.show("开始失败");
                    }
                } else {
                    audioRecordTool.isRecording = false;
                    btHandler.setText("开始");
                    ToastUtils.show("停止录音");
                }
            }
        });

        audioRecordTool = new AudioRecordTool();
        audioRecordTool.setOnDecibelListener(new AudioRecordTool.OnDecibelListener() {
            @Override
            public void onDecibel(double decibel) {
                tvDb.setText("db:" + (int) decibel);
            }
        });
        audioRecordTool.setOnRecordListener(new AudioRecordTool.OnRecordListener() {
            @Override
            public void onError(IOException e) {
                state = 0;
                btHandler.setText("开始");

                ToastUtils.show("错误");
            }

            @Override
            public void onComplete(String pcmPath) {
                state = 0;
                btHandler.setText("开始");

                ToastUtils.show("录音完成");

                tvPcmSize.setText("size: " + new File(pcmPath).length() + "");
            }
        });
    }
}